package vn.vnu.uet.email;

import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

import vn.vnu.uet.email.R;
import vn.vnu.uet.email.bean.User;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Login extends Activity {
	// AlertDialog.Builder alertbox = new Builder(Login.this);
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		Token.instance();
		Token.clear();
		Token.instance().user = new User();
		final EditText user = (EditText) findViewById(R.id.user);
		final EditText pass = (EditText) findViewById(R.id.password);


		Button btn = (Button) findViewById(R.id.sign_in_button);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String host = "ctmail.vnu.edu.vn";// change accordingly
				String mailStoreType = "pop3";
				String username = user.getText().toString();
				String password = pass.getText().toString();// change
															// accordingly

				new ProgressTask(Login.this).execute(host, mailStoreType,
						username, password);

			}
		});
	}

	// load email
	protected class ProgressTask extends AsyncTask<String, Void, Boolean> {
		private ProgressDialog dialog;

		ProgressTask(Context c) {
			super();
			dialog = new ProgressDialog(c);
		}

		protected void onPreExecute() {
			this.dialog.setMessage("Đang đăng nhập \nVui lòng đợi");
			this.dialog.show();
		}

		protected void onPostExecute(final Boolean success) {

			if (dialog.isShowing()) {
				dialog.dismiss();
				if (success.equals(true))
					ViewController.showInbox(Login.this);
				else {
					TextView tv = (TextView) findViewById(R.id.textView1);
					tv.setText("Tên đăng nhập hoặc mật khẩu sai!");
				}
			}
		}

		protected Boolean doInBackground(final String... args) {
			try {
				String host = args[0];// change accordingly
				String mailStoreType = args[1];
				String username = args[2];
				String password = args[3];// change accordingly

				Properties properties = System.getProperties();
				properties.setProperty("mail.pop3.disabletop", "true");

				Session emailSession = Session.getDefaultInstance(properties);

				Store emailStore = emailSession.getStore(mailStoreType);
				try {
					emailStore.connect(host, username, password);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				Folder emailFolder = emailStore.getFolder("inbox");
				emailFolder.open(Folder.READ_ONLY);
				System.out.println("---------------------");

				Token.instance().user = new User(null, username, null, null,
						null, username + "@vnu.edu.vn", password);
				Message[] messages = emailFolder.getMessages();
				Token.instance().host = host;
				Token.instance().mailStoreType = mailStoreType;
				Token.instance().messages = messages;

//				emailFolder.close(false);
//				emailStore.close();
				return true;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			// Press Enter do nothing
			android.os.Process.killProcess(android.os.Process.myPid());
			finish();
			return true;

		}
		return super.onKeyDown(keyCode, event);
	}
}
